package stringutils

import "fmt"

// 拼接update的sql语句
func UpdateSql(tableName string, data map[string]interface{}, conditon string) string {
	field := ""
	for k, v := range data {
		if field == "" {
			field = k + "='" + fmt.Sprintf("%v", v) + "'"
		} else {
			field = field + "," + k + "='" + fmt.Sprintf("%v", v) + "'"
		}
	}
	return "UPDATE " + tableName + " SET " + field + " WHERE " + conditon
}

func DeleteSql(tableName string, condition string) string {
	return "DELETE FROM " + tableName + " WHERE " + condition
}

func InsertSql(tableName string, data map[string]interface{}) string {
	field := ""
	values := ""
	for k, v := range data {
		if field != "" {
			field = field + "," + fmt.Sprintf("%v", k)
			values = values + "," + "'" + fmt.Sprintf("%v", v) + "'"
		} else {
			field = fmt.Sprintf("%v", k)
			values = "'" + fmt.Sprintf("%v", v) + "'"
		}
	}
	return "INSERT IGNORE INTO " + tableName + "(" + field + ") VALUES(" + values + ")"
}

func InsertOrUpdateSql(tableName string, data map[string]interface{}) string {
	sql := InsertSql(tableName, data) + " ON DUPLICATE KEY UPDATE "

	field := ""
	for k, v := range data {
		if field != "" {
			field = field + ", " + k + fmt.Sprintf("=%v", v)
		} else {
			field = k + fmt.Sprintf("=%v", v)
		}
	}
	return sql + field

}
